package jiuxing

import "github.com/zeromicro/go-zero/core/logx"

type Plug struct {
	Voltage    uint16
	Current    uint16
	Pwr        uint32
	ModVoltage uint16
	ModCurrent uint16
	ModTemp    byte
	PlugTemp   byte
}

type Msg0A struct {
	Tm       []byte
	Voltage  []uint16
	Current  []uint16
	TotalPwr uint32
	Temp     byte
	TempIn   byte
	TempOut  byte
	TempCtl  byte
	Humi     byte
	PlugNum  byte
	Plugs    []Plug
}

func (msg *Msg0A) Decode(b []byte) {
	item := NewByteArray(b)
	msg.Tm = item.GetTimeArray(0)
	msg.Voltage = make([]uint16, 3)
	msg.Current = make([]uint16, 3)
	msg.Voltage[0] = item.GetUint16(6)
	msg.Voltage[1] = item.GetUint16(8)
	msg.Voltage[2] = item.GetUint16(10)
	msg.Current[0] = item.GetUint16(12)
	msg.Current[1] = item.GetUint16(14)
	msg.Current[2] = item.GetUint16(16)
	msg.TotalPwr = item.GetUint32(18)
	msg.Temp = item.GetUint8(22)
	msg.TempIn = item.GetUint8(23)
	msg.TempOut = item.GetUint8(24)
	msg.TempCtl = item.GetUint8(25)
	msg.Humi = item.GetUint8(26)
	msg.PlugNum = item.GetUint8(35)
	msg.Plugs = make([]Plug, 0)
	for i := 0; i < int(msg.PlugNum); i++ {
		idx := 16 + 18*i
		plug := Plug{
			Voltage:    item.GetUint16(idx),
			Current:    item.GetUint16(idx + 2),
			Pwr:        item.GetUint32(idx + 6),
			ModVoltage: item.GetUint16(idx + 8),
			ModCurrent: item.GetUint16(idx + 10),
			ModTemp:    item.GetUint8(idx + 11),
			PlugTemp:   item.GetUint8(idx + 12),
		}
		msg.Plugs = append(msg.Plugs, plug)
	}
	logx.Infof("%+v", msg)
}
